// 将 app 定义成一个 AMD 模块， 依赖于 angular ， 这样 RequireJS
// 加载 app 时会自动加载 angular 。
define(['angular', 'angular-route', 'jquery', 'bootstrap'], function(
				angular) {

			// 定义 angular 模块
			var app = angular.module('app', ['ngRoute']);
			
			app.config(['$routeProvider', function($routeProvider) {
						$routeProvider.when('/', {
									controller : 'indexCtrl'
								}).when('/computers', {
									template : '这是电脑分类页面'
								}).when('/printers', {
									template : '这是打印机页面'
								}).when('/blabla', {
									template : '这是其他页面'
								}).otherwise({
									redirectTo : '/'
								});
					}]);
		
			app.controller('indexCtrl', function($scope, $http) {
				$scope.isindex = false;
				$scope.islogin = false;
				$scope.getnavigation = function() {
					$http({
								method : 'GET',
								url : getRootPath() + "/getNavigation",
								headers:{
									"build-auth-token": sessionStorage.getItem("build-auth-token")
								}
							}).then(function successCallback(response) {
								console.log(response);
								if ("success" == response.data.state) {
									$scope.navbar = response.data.result;
									$scope.isindex = true;
									$scope.islogin = false;
								} else {
									$scope.islogin = true;
									$scope.isindex = false;
								}
							}, function errorCallback(response) {
								console.log("errorCallback"
										+ response.data);
							});
				};

				$scope.getnavigation();

				$scope.login = function() {
					$http({
								method : 'POST',
								url : getRootPath() + "/login",
								params : {
									"principal" : $scope.username,
									"credentials" : $scope.password
								},
								headers:{
                                    "build-auth-token": sessionStorage.getItem("build-auth-token")
								}
							}).then(function successCallback(response, headers) {
								console.log(response);
								if ("success" == response.data.state) {
                                    sessionStorage.setItem("build-auth-token",response.headers("build-auth-token"))
									$scope.isindex = true;
									$scope.islogin = false;
									$scope.getnavigation();
								} else {
									$scope.msg = response.data.message;
									$scope.islogin = true;
									$scope.isindex = false;
								}
							}, function errorCallback(response) {
								console.log("errorCallback" + response);
							});
				};
			});

			return app;
		});